import { http } from "@/utils/http";
import { getToken175 } from "@/utils/token";

type Result = {
  message: string;
  data: any;
  code: number;
};

// 为task API添加Authorization头的辅助函数
const taskRequest = <T>(method: string, url: string, data?: object) => {
  const token = getToken175(); // 每次调用都获取最新的token
  return http.request<T>(method as any, url, {
    data,
    headers: {
      Authorization: token ? `Bearer ${token}` : undefined,
    },
  });
};

// 查询日志列表接口
export const getQaLogs = (data?: {
  page?: number;
  per_page?: number;
  date_range?: "today" | "yesterday" | "7d" | "30d" | "custom";
  start_date?: string; // custom时使用
  end_date?: string; // custom时使用
  log_type?: "all" | "qa" | "graph";
  used_ai?: boolean | null; // true, false, null(全部)
  min_response_time?: number | null; // 最小响应时间(ms)
  user_id?: string | null;
}) => {
  return taskRequest<Result>("post", "/api/qa/logs", data);
};

// 查询日志统计接口
export const getQaLogsStats = (data?: {
  date_range?: "today" | "yesterday" | "7d" | "30d" | "custom";
  start_date?: string;
  end_date?: string;
}) => {
  return taskRequest<Result>("post", "/api/qa/logs/stats", data);
};
